<?php

if(isset($_POST["register"]))
    if($Users->register_into($_POST["group"],$user))
        header("Location: /?page=registrations");

?>
<div id="content">
    <h2>Ilmoittaudu kursseille</h2>
    <ul>
        <? $courses = $Courses->get_available_to($user);
        foreach($courses as &$course)
        {
            if($course->teacher != null)
                $teacher = " (" . e($course->teacher) . ")";
            else
                $teacher = null;
            ?>
            <li>
                <h3><a href="javascript:void(0);" onclick="toggle(this)" class="toggle"><? echo e($course->name); ?></a></h3>
                <ul style="display:none;">
                    <li>
                        <h4>Luennot<? echo $teacher; ?></h4>
                        <table class="timetable">
                            <thead>
                                <tr>
                                    <th class="number">#</th>
                                    <th class="date">Päivä</th>
                                    <th class="time">Aika</th>
                                    <th class="location">Paikka</th>
                                </tr>
                            </thead>
                            <tbody>
                                <? $i = 0; $lectures = $Lectures->get_by_instance($course->instance);
                                foreach($lectures as &$lecture) { ++$i; ?>
                                    <tr>
                                        <td class="number"><? echo $i; ?></td>
                                        <td class="date"><? echo date("d.m.Y",$lecture->date); ?></td>
                                        <td class="time"><? echo date("H:i",$lecture->start_time); ?>—<? echo date("H:i",$lecture->end_time); ?></td>
                                        <td class="location"><? echo e($lecture->location); ?></td>
                                    </tr>
                                <? } ?>
                            </tbody>
                        </table>
                    </li>
                    <li>
                        <h4>Kokeet</h4>
                                    <table class="timetable">
                                        <thead>
                                            <tr>
                                                <th class="number">#</th>
                                                <th class="date">Päivä</th>
                                                <th class="time">Aika</th>
                                                <th class="location">Paikka</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?
                                            $i = 0;
                                            $exams = $Exams->get_by_instance($course->instance);
                                            foreach($exams as &$exam) 
                                            { 
                                            ++$i; 
                                            ?>
                                                <tr>
                                                    <td class="number"><? echo $i; ?></td>
                                                    <td class="date"><? echo date("d.m.Y",$exam->date); ?></td>
                                                    <td class="time"><? echo date("H:i",$exam->start_time); ?>—<? echo date("H:i",$exam->end_time); ?></td>
                                                    <td class="location"><? echo e($exam->location); ?></td>
                                                </tr>
                                            <? } ?>
                                        </tbody>
                                    </table>
                    </li>
                    <li>
                        <h4>Ryhmät</h4>
                        <ol>
                            <? $groups = $Groups->get_by_instance($course->instance);
                            foreach($groups as &$group) { ?>
                                <li>
                                    <h5><? echo e($group->superviser_name); ?></h5>
                                    <table class="timetable">
                                        <thead>
                                            <tr>
                                                <th class="number">#</th>
                                                <th class="date">Päivä</th>
                                                <th class="time">Aika</th>
                                                <th class="location">Paikka</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <? $i = 0; $meetings = $Meetings->get_by_group($group->id);
                                            foreach($meetings as &$meeting) { ++$i; ?>
                                                <tr>
                                                    <td class="number"><? echo $i; ?></td>
                                                    <td class="date"><? echo date("d.m.Y",$meeting->date); ?></td>
                                                    <td class="time"><? echo date("H:i",$meeting->start_time); ?>—<? echo date("H:i",$meeting->end_time); ?></td>
                                                    <td class="location"><? echo e($meeting->location); ?></td>
                                                </tr>
                                            <? } ?>
                                        </tbody>
                                    </table>
                                    <form method="post" action="index.php?page=register">
                                        <input type="hidden" name="group" value="<? echo e($group->id); ?>" />
                                        <input type="submit" name="register" value="Ilmoittaudu tähän ryhmään" />
                                    </form>
                                </li>
                            <? } ?>
                        </ol>
                    </li>
                </ul>
            </li>
        <? } ?>
    </ul>
</div>
